package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.Manager;
import com.qf.service.ManagerService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Manager)表控制层
 *
 * @author makejava
 * @since 2024-04-30 09:50:20
 */
@RestController
@RequestMapping("/api/manager")
public class ManagerController {
    /**
     * 服务对象
     */
    @Autowired
    private ManagerService managerService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public R queryByPage(Long pageNum) {
        return R.ok(this.managerService.queryByPage(pageNum));
    }

    @RequestMapping("/list/page")
    public R selectManagerByCondition(@RequestParam Map<String,Object> map) {
        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<Manager> list = managerService.selectManagerByCondition(map);
        if (list != null && !list.isEmpty()) {
            PageInfo<Manager> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }

        return R.fail();
    }

    @RequestMapping("/selectAll")
    public List<Manager> selectAllManager() {
        return managerService.selectAllManager();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.managerService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param manager 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(Manager manager, HttpSession session) {
        session.setAttribute("manager",manager);
        boolean result = managerService.insert(manager,session);
        if (result) {
            return R.ok();
        }
        return R.fail("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param manager 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(Manager manager) {
        boolean result = managerService.update(manager);
        if (result) {
            return R.ok();
        }
        return R.fail("编辑失败");
    }

    /**
     * 删除数据
     *
     * @param account 行条件
     * @return 删除是否成功
     */
    @RequestMapping("/del/{account}")
    public R deleteByAccount(@PathVariable String account) {
        boolean result = managerService.deleteByAccount(account);
        if (result) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list) {
        boolean result = managerService.deleteBatch(list);
        if (result) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 用户登陆,要保存登陆状态更新
     * @param manager
     * @param session
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public R login(Manager manager, HttpSession session, HttpServletRequest request) {

        Manager mgr = managerService.login(manager,request);

        if (mgr != null) {
            session.setAttribute("manager",mgr);
            return R.ok(mgr);
        }
        return R.fail("失败");

    }

    /**
     * 数据导出文件
     * @param response
     */
    @RequestMapping("/managerExport")
    public void managerExport(HttpServletResponse response) {
        managerService.managerExport(response);
    }

    /**
     * 导入数据文件
     * @param file
     * @return
     */
    @RequestMapping("/importManager")
    public R importManager(MultipartFile file) {
        boolean result = managerService.importManager(file);
        if (result) {
            return R.ok();
        }
        return R.fail();
    }


}

